import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_ai_dart/routers/app_pages.dart';
import 'package:smart_ai_dart/utils/log_utils.dart';
import 'package:smart_ai_dart/utils/storage.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';

class LoginController extends GetxController {
  final GlobalKey<FormBuilderState> formKey = GlobalKey<FormBuilderState>();
  var isLoading = false.obs; // 控制加载状态
  var isPasswordVisible = false.obs; // 是否显示密码
  final String defaultUsername = 'test@example.com';
  final String defaultPassword = 'SQ123456pl';

  void togglePasswordVisibility() {
    LogUtils.shared.d('isPasswordVisible.value');
    isPasswordVisible.value = !isPasswordVisible.value;
  }

  void login() async {
    LogUtils.shared.d(formKey.currentState?.value);
    if (formKey.currentState?.saveAndValidate() ?? false) {
      // TODO: 实际的登录API调用
      // 获取表单数据- 实际的登录API调用
      final formData = formKey.currentState?.value;
      String username = formData?['username'];
      String password = formData?['password'];
      LogUtils.shared.d(username);
      LogUtils.shared.d(password);
      LogUtils.shared.d(formKey.currentState?.value);

      Get.snackbar(
        'success'.tr,
        'login_success_msg'.tr,
      );
      await StorageUtils.shared.setString('jwt_token', 'dummy_token');
      Get.offAllNamed(Routes.MAIN, arguments: {
        'tab_index': 1,
        'username': username,
      });
    } else {
      Get.snackbar(
        'login_error'.tr,
        'login_error_msg'.tr,
      );
    }
  }
}
